﻿using System.IO;
using System.Security.Cryptography;

namespace Codemonk.Updater
{
    /// <summary>
    /// 文件的hash辅助类
    /// </summary>
    internal class FileHashHelper
    {
        /// <summary>
        /// 使用MD5
        /// </summary>
        private static MD5 md5 = MD5.Create();
        private static string ComputeHash(Stream stream)
        {
            try
            {
                return System.BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty);
            }
            catch
            {
                return null;
            }
        }
        public static string ComputeHash(string file)
        {
            try
            {
                using (var stream = System.IO.File.OpenRead(file))
                {
                    return ComputeHash(stream);
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
